package com.qys.livesMall.shop.controller.app;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.product.service.H2ProductSkuAttributeService;
import com.qys.livesMall.shop.modal.SeckillGoodsDTO;
import com.qys.livesMall.shop.modal.dto.SeckillActivityAppDto;
import com.qys.livesMall.shop.modal.dto.ShopSeckillOrderDto;
import com.qys.livesMall.shop.modal.dto.shop.order.SeckillAddOrder;
import com.qys.livesMall.shop.modal.entity.*;
import com.qys.livesMall.shop.modal.query.QuerySeckillActivityApp;
import com.qys.livesMall.shop.modal.query.QuerySeckillGoodsApp;
import com.qys.livesMall.shop.modal.vo.SeckillActivityAppVO;
import com.qys.livesMall.shop.service.*;
import com.qys.livesMall.shop.service.api.impl.ShopOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 *
 * @author gmx
 * @since 2023-07-03
 */
@RestController
@RequestMapping("/app/seckillActivity")
@Tag(name = "app-秒杀活动")
public class AppSeckillActivityController {
    @Resource
    private SeckillActivityService seckillActivityService;

    @Resource
    private SeckillGoodsService seckillGoodsService;

    @Resource
    private ShopGoodsInfoService goodsInfoService;
    @Resource
    private ShopGoodsSkuService goodsSkuService;
    @Resource
    private ShopGoodsSkuStocksService stocksService;
    @Resource
    private ShopOrderService shopOrderService;
    @Resource
    private H2ProductSkuAttributeService attributeService;
    /***
     * 未过期的活动列表
     */
    @Operation(summary = "未过期的活动列表首页")
    @PostMapping("/oneActivity")
    public R<SeckillActivityAppDto> oneActivity(@RequestBody QuerySeckillActivityApp seckillActivityApp){
        seckillActivityApp.setNow(System.currentTimeMillis());
        //有效的活动时间查询
        List<SeckillActivityAppDto> list = seckillActivityService.validActivity(seckillActivityApp);
        SeckillActivityAppDto seckillActivityAppDto = new SeckillActivityAppDto();
        if (ObjectUtils.isNotEmpty(list)){
            seckillActivityAppDto=list.get(0);
        }
        return R.ok(seckillActivityAppDto);
    }
    @Operation(summary = "未过期的活动列表")
    @PostMapping("/query")
    public R<List<SeckillActivityAppDto>> list(@RequestBody QuerySeckillActivityApp seckillActivityApp){
        seckillActivityApp.setNow(System.currentTimeMillis());
        //有效的活动时间查询
        List<SeckillActivityAppDto> list = seckillActivityService.validActivity(seckillActivityApp);
        return R.ok(list);
    }
    @Operation(summary = "活动商品")
    @PostMapping("/goodList")
    public R<Page<SeckillGoods>> goodList(@RequestBody QuerySeckillGoodsApp goodsApp){
        return R.ok(seckillGoodsService.queryGoods(goodsApp));
    }
    @PostMapping("/info")
    public R<ShopProductSeckill> info(@RequestBody SeckillGoodsDTO goods){
        List<Long> shopIds=goods.getShopIds();
        if(CollectionUtils.isEmpty(shopIds)){
            return R.fail(MessageUtils.message("system.error"));
        }
        //获取有库存的shopId
        shopIds=stocksService.getShopIdByNum(Long.valueOf(goods.getSkuId()),shopIds);
        //取一个随机店铺 打乱顺序
        Collections.shuffle(shopIds);
        Long shopId=shopIds.get(0);
        ShopGoodsSku goodSku = goodsSkuService.getGoodsSku(goods.getSkuId().toString(), shopId);
        //获取活动对应商品
        SeckillGoods seckillGoods=seckillGoodsService.getSku(goods.getSkuId(),goods.getActivityId());

        ShopGoodsSkuStocks stocks=null;
        ShopProductSeckill shopProduct=new ShopProductSeckill();
        if (goodSku!=null) {
            seckillGoods.setSkuName(attributeService.getSkuName(goodSku.getSkuAttr()));
            stocks = stocksService.queryBySkuId(goodSku.getId());

            if (stocks != null) {
                ShopGoodsInfo goodInfo = goodsInfoService.getById(goodSku.getShopGoodsId());
                shopProduct.setGoodsInfo(goodInfo);

                goodSku.setOriginalPrice(seckillGoods.getPrice());
                goodSku.setPrice(seckillGoods.getSeckillPrice());
                goodSku.setAvailableNum(stocks.getNum());
                shopProduct.setGoodsSku(goodSku);
                shopProduct.setSeckillGoods(seckillGoods);
                return R.ok(shopProduct);
            }
        }
        if (stocks == null) {
            // 没有找到符合条件的结果，返回 false
            return R.fail(MessageUtils.message("shop.stocks.null"));
        }
        return R.ok();
    }
    @Operation(summary = "生成秒杀订单")
    @PostMapping("/addSeckillOrder")
    @Transactional(rollbackFor = {Exception.class})
    public R<ShopOrder> addSeckillOrder(@RequestBody SeckillAddOrder addOrder){
        addOrder.setUserId(SecurityUtils.getUserId());
        addOrder.setUserName(SecurityUtils.getUsername());
        return shopOrderService.add(addOrder);
    }

    @GetMapping("/getSeckillInfo/{activityId}")
    @Operation(summary = "获取秒杀活动信息")
    public R<SeckillActivityAppVO> getSeckillInfo(@PathVariable Long activityId){
        return R.ok(seckillActivityService.getSeckillInfo(activityId));
    }

}
